import tornado.web
import tornado.websocket
from util.db import db
from util.redis import r
import json,time
conndict = {}
class WebsockHandler(tornado.websocket.WebSocketHandler):
    # 跨域支持
    def check_origin(self, origin):
        return True
    # 建立连接
    def open(self):
        # 获取参数
        id = int(self.get_query_argument('id'))
        # 存储连接
        conndict[id] = self
    #接收消息
    def on_message(self,mes):
        print(mes)
    def on_close(self):
        pass


class TTHandler(tornado.web.RequestHandler):
    def get(self):
        msg = self.get_query_argument('mes')
        id = int(self.get_query_argument('id'))
        conndict[id].write_message(msg)

class TT(tornado.web.RequestHandler):
    def get(self):
        id = int(self.get_query_argument('id'))
        while True:
            key1 = r.get_str('key1')
            if key1:
                key1 = key1.decode()
                key2 = r.get_str('key2')
                key3 = r.get_str('key3')
                if key1:
                    key1 = key1.decode()
                    sql = "select * from shi where name='%s'"%('key1')
                    res = db.fetchone(sql)
                    status = 1
                    if key1<res['min'] or key1>res['max']:
                        status = 2
                    conndict[id].write_message({'code':200,'wendu':key1,'status':status})
        #         time.sleep(1)
# class Math():
#     pass
# a = [10,555,666,1000,654,789,61,3]
# k = [5,6,7,9,6666,555]
# print(a,k)
# print()
# print(max(a))
# print(min(a))


# import tornado.websocket
# import tornado.web
# conndict={}
# class WebsockHandler(tornado.websocket.WebSocketHandler):
#     # 跨域支持
#     def check_origin(self, origin):
#         return True
#     #建立连接
#     def open(self):
#         #获取参数
#         id = int(self.get_query_argument("id"))
#         #存储链接
#         conndict[id]=self
#     #接收消息
#     def on_message(self,mes):
#         print(mes)
#
#     def on_close(self):
#         pass
#
#
# class TTHandler(tornado.web.RequestHandler):
#     def get(self):
#         msg = self.get_query_argument("mes")
#         id = int(self.get_query_argument("id"))
#         conndict[id].write_message(msg)